/*
 * Copyright (c) 2009, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @addtogroup event
 * @{
 * @defgroup events The LooCI Event Bus
 * @{
  */
/**
 * @file 
 * Header file for the LooCI Event Bus
 * @author
 * Wouter Horré <wouter.horre@cs.kuleuven.be>
 */
#ifndef __LOOCI_EVENTS_H__
#define __LOOCI_EVENTS_H__

#include "contiki.h"
#include "lib/memb.h"
#include "eventtype.h"
#include "lib/list.h"
#include "net/peers.h"
#include <string.h>

/**
 * @name Return values
 * @{
 */
/**
 * @brief Successful
 *
 *        This value is returned by the LooCI Event Bus if an operation
 *        was successful.
 *
 * @hideinitializer
 */
#define LOOCI_SUCCESS 0
/**
 * @brief Event could not be published
 *
 *        This value is returned by the LooCI Event Bus if an event could
 *        not be published successfully.
 * @hideinitializer
 */
#define LOOCI_ERR_EVENTNOTPUBLISHED -1
/** @} */

/**
 * @brief The maximum length of a LooCI event payload.
 *
 *        The payload of a LooCI cannot be larger than
 *        this constant. If a component tries to publish
 *        an event with a larger payload, the passed payload
 *        will be truncated!
 *
 * @hideinitializer
 *
 * @todo is this large enough?
 */
#define LOOCI_EVENT_PAYLOAD_MAXLEN 40

/**
 * @name Contiki definitions
 * @brief Definitions and macro calls needed for Contiki
 * @{
 */
/**
 * @brief The name of the Contiki process for the LooCI Event Manager
 */
PROCESS_NAME(looci_event_manager);
/**
 * The Contiki event number for looci related Contiki events
 */
process_event_t looci_process_event;
/**
 * The Contiki event number used for delaying a component that receives an event.
 */
process_event_t looci_delay_event;
/**
 * The Contiki event number for remote looci events
 */
process_event_t looci_remote_event;
/** @} */

struct looci_event {
  looci_eventtype_t type;
  peer_id_t source_node;
  u8_t source_cid;
  u8_t len;
  char payload[LOOCI_EVENT_PAYLOAD_MAXLEN];
};

/**
 * Publish a LooCI event.
 *
 * @param type The event type.
 * @param payload The event payload.
 * @param len The length of the payload.
 * 
 * @retval LOOCI_SUCCESS The event was published successful
 * @retval LOOCI_ERR_EVENTNOTPUBLISHED The event could not be published
 *
 */
CCIF int looci_event_publish(looci_eventtype_t type, void * payload, u8_t len);

/**
 * Wait until  an event is received.
 *
 * @param event Pointer to the event after the event is received. This pointer
 *              should point to an allocated event structure. The event will be
 *              copied into the memory pointed to by the pointer.
 *
 * @hideinitializer
 */
// The do { } while() construction is needed because we can't use the PROCESS_WAIT_EVENT_UNTIL macro twice
// in the same macro.
// We could define this one in terms of LOOCI_EVENT_RECEIVE_UNTIL, but since this is a macro and will
// be included verbatim into components that use it, we go for the shortest form.
#define LOOCI_EVENT_RECEIVE(event) do { \
  PROCESS_WAIT_EVENT_UNTIL(ev == looci_process_event || ev == looci_delay_event); \
  if(ev == looci_process_event) { \
    memcpy(event, data, sizeof(struct looci_event)); \
    process_post(PROCESS_CURRENT(), looci_delay_event, NULL);\
  }\
} while(ev != looci_delay_event)

/**
 * Wait until an event is received or condition is true.
 *
 * @param event Pointer to the event after the event is received. This pointer
 *              should point to an allocated event structure. The event will be
 *              copied into the memory pointed to by the pointer.
 * @param condition Alternative condition that will interupt the waiting for an event
 *                  when the condition becomes true.
 *
 * @hideinitializer
 */
// The do { } while() construction is needed because we can't use the PROCESS_WAIT_EVENT_UNTIL macro twice
// in the same macro.
#define LOOCI_EVENT_RECEIVE_UNTIL(event, condition) do { \
  PROCESS_WAIT_EVENT_UNTIL(ev == looci_process_event || ev == looci_delay_event || (condition)); \
  if(condition) break;\
  if(ev == looci_process_event) { \
    memcpy(event, data, sizeof(struct looci_event)); \
    process_post(PROCESS_CURRENT(), looci_delay_event, NULL);\
  }\
} while(ev != looci_delay_event)

/**
 * Condition to test whether an event is received. 
 * Can be used to test whether LOOCI_EVENT_RECEIVE_UNTIL stopped because 
 * an event was received.
 *
 * For example:
 * LOOCI_EVENT_RECEIVE_UNTIL(event, etimer_expired(&et));
 * if(LOOCI_EVENT_RECEIVED) {
 *   do something with event
 * } else if(etimer_expired(&et)) {
 *   do something else
 * }
 *
 * @hideinitializer.
 */
#define LOOCI_EVENT_RECEIVED (ev == looci_delay_event)

/**
 * Structure that holds the information for an incoming remote wire.
 */
struct wire_from {
  peer_id_t source_node;
  u8_t source_cid;
};

#endif /* __LOOCI_EVENTS_H__ */
/** @} */
/** @} */
